package com.ckv.qcm.view.Themes;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.border.Border;

import com.ckv.qcm.model.Theme;
import com.ckv.qcm.view.Utils.EditElementPanelHandler;
import com.ckv.qcm.view.Utils.ElementPanel;


public class ThemePanel extends ElementPanel
{		

	private Theme theme;
	private Border border;
	private JLabel label;

	private JTextField textField;
	private JButton buttOK;

	private ArrayList<EditElementPanelHandler> editHandlers = new ArrayList<EditElementPanelHandler>();

	public ThemePanel(Theme theme, Mode mode) 
	{
		super();
		this.theme = theme;
		this.mode = mode;
		initComponent();
	}

	public ThemePanel() 
	{
		super();
		this.mode = Mode.CREATE;
		initComponent();
	}

	public ThemePanel(int width, int height, Theme theme, Mode mode)
	{
		super(width, height);
		this.theme = theme;
		this.mode = mode;
		initComponent();
	}

	public ThemePanel(int width, int height) 
	{
		super(width, height);
		this.mode = Mode.CREATE;
		initComponent();
	}
	
	public void addEditHandler(EditElementPanelHandler handler)
	{

		this.editHandlers.add(handler);
	}

	public void switchMode(Mode mode) 
	{
		if (mode != this.mode) 
		{
			this.removeAll();
			this.mode = mode;
			this.initComponent();
			validate();
		}
	}

	private void initComponent()
	{

		this.border = BorderFactory.createLineBorder(Color.black);
		this.setLayout(new FlowLayout(FlowLayout.LEFT));

		if (this.mode == Mode.CREATE || this.mode == Mode.EDIT) {

			this.textField = new JTextField(20);
			// this.textField.setPreferredSize(new Dimension(this.getWidth() -
			// 40,this.getHeight() -20));
			this.textField.setAlignmentY(CENTER_ALIGNMENT);

			if (this.theme != null)
			{
				this.textField.setText(this.theme.getName());
			}

			// this.textField.setPreferredSize(new Dimension(this.getWidth() -
			// 50,this.getHeight() - 10));
			// this.textField.setMargin(new Insets(5, 5, 5, 10));

			this.buttOK = new JButton("Ok");
			this.buttOK.setAlignmentY(CENTER_ALIGNMENT);
			// this.buttOK.setMargin(new Insets(5, 15, 5, 10));
			this.buttOK.addActionListener(new ActionListener() 
			{
				@Override
				public void actionPerformed(ActionEvent e)
				{
					validText();
				}
			});

			this.add(this.textField);
			this.add(this.buttOK);
		} 
		else if (mode == Mode.READ) 
		{
			System.out.println("MODE READ");
			this.label = new JLabel(theme.getName());
			this.label.setAlignmentY(CENTER_ALIGNMENT);
			this.label.setVerticalAlignment(JLabel.CENTER);

			this.add(label);
		}

		this.setBorder(border);

	}

	public Theme getTheme() 
	{

		return this.theme;
	}

	public void setTheme(Theme theme)
	{
		this.theme = theme;
	}

	private void validText() 
	{
		String text = this.textField.getText();
		System.out.println("TEXT : " + text);
		System.out.println("NB Handler : " + editHandlers.size());
		for (EditElementPanelHandler handler : editHandlers) {
			if (this.mode == Mode.CREATE)
				handler.onValidCreateElement(text, this);
			else if (this.mode == Mode.EDIT)
				handler.onValidEditElement(text, this);
		}

	}

	@Override
	public void focus()
	{
		if(this.textField != null)
			this.textField.requestFocus();
		
	}
	
}
